package com.example.guiguiapplicatio.fragment.followup.adapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.FragmentFollowUpRecyItemBinding;
import com.example.guiguiapplicatio.databinding.FragmentNewFollowUpRecyItemBinding;
import com.example.guiguiapplicatio.databindingBean.followup.ListFollowupWith;
import com.example.guiguiapplicatio.utils.BirthdayUtils;

import java.util.List;

public class FollowUpReportFragmentAdapter extends RecyclerView.Adapter<FollowUpReportFragmentAdapter.ViewHolder> {
    private List<ListFollowupWith> mList;
    private NavController navController;
    private String followup_type;
    public  FollowUpReportFragmentAdapter(List<ListFollowupWith> mList,String followup_type){
        this.followup_type=followup_type;
        this.mList=mList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        navController= Navigation.findNavController(parent);
        return new ViewHolder(FragmentFollowUpRecyItemBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.mItem=mList.get(position);
        holder.binding.age.setText(BirthdayUtils.extractYearMonthDayOfIdCard(holder.mItem.getFollowUp().getIdCard()));
        holder.binding.sex.setText(holder.mItem.getSex());
        holder.binding.name.setText(holder.mItem.getPatientName());
        holder.binding.phone.setText(holder.mItem.getPatientPhone());
        if (holder.mItem.getFollowUp().getIdCard().length()>16){

            String show_id = holder.mItem.getFollowUp().getIdCard().substring(0, 3) + "********" + holder.mItem.getFollowUp().getIdCard().substring(11);
            holder.binding.idcard.setText(show_id);
        }else  holder.binding.idcard.setText(holder.mItem.getFollowUp().getIdCard());
        holder.binding.date.setText(holder.mItem.getFollowUp().getEditTime());
        holder.binding.adress.setText(holder.mItem.getPatientResidence());
        holder.binding.type.setText(holder.mItem.getFollowUp().getFollowUpType());

        if (holder.mItem.getFollowUp().getState()==1){
            holder.binding.state.setText("正式随访");

        }else if (holder.mItem.getFollowUp().getState()==2){
            holder.binding.state.setText("已作废");
            holder.binding.headStye.setBackgroundResource(R.drawable.border_head_gray);
            holder.binding.background.setBackgroundResource(R.drawable.border_background_gray);
        }
        holder.binding.recyItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle=new Bundle();
                bundle.putSerializable("ListFollowupWith",holder.mItem);
               if (followup_type.equals("新生儿")){
                   navController.navigate(R.id.action_followUpReportFragment_to_followUpReportNewChildFragment,bundle);
               }else if (followup_type.equals("高血压")){
                   navController.navigate(R.id.action_followUpReportFragment_to_followUpReportHypertensionFragment,bundle);
               }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ListFollowupWith mItem;
        FragmentFollowUpRecyItemBinding binding;
        public ViewHolder(@NonNull FragmentFollowUpRecyItemBinding binding) {
            super(binding.getRoot());
            this.binding=binding;

        }
    }
}
